﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.ApproveCommand;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.ApproveCommandHandler
{
    /// <summary>
    /// 请假申请表显示-命令
    /// </summary>
    public class ListLeaveModelCommandHandler : IRequestHandler<ListLeaveModelCommand, APIPaing<LeaveModel>>
    {
        private readonly IBaseRepository<LeaveModel> leaveRepos;
        private readonly IBaseRepository<User> userRepos;

        public ListLeaveModelCommandHandler(IBaseRepository<LeaveModel> leaveRepos, IBaseRepository<User> userRepos)
        {
            this.leaveRepos = leaveRepos;
            this.userRepos = userRepos;
        }

        public async Task<APIPaing<LeaveModel>> Handle(ListLeaveModelCommand request, CancellationToken cancellationToken)
        {
            APIPaing<LeaveModel> result = new APIPaing<LeaveModel>();

            var list = await leaveRepos.GetAll().Result.Where(x => x.IsDeleted == false).ToListAsync();
            //取到申请人以上的层级(包含自己)
            var roles = await userRepos.GetAll().Result.Where(x => x.LevelRoleId <= request.LevelRoleId).ToListAsync();
            //层级数转换数组
            var rolestring = roles.Select(x => x.LevelRoleId).ToArray();
            //判断条件 可查看人(本人,上级所有领导 --(看不到未通过的) )

            list = list.Where(x => x.UserId == request.UserId || (rolestring.Contains(x.LevelRoleId) && x.DepId == request.DepId/* && x.AppStatus != 3*/)).ToList();
            //////条件查询///////
            //申请人名称
            if (!string.IsNullOrEmpty(request.NickName))
            {
                list = list.Where(x => x.NickName.Contains(request.NickName)).ToList();
            }
            //审批状态
            if (request.AppStatus != null)
            {
                list = list.Where(x => x.AppStatus == request.AppStatus).ToList();
            }
            //创建时间查询
            if (request.StartTime != null && request.EndTime != null)
            {
                list = list.Where(x => x.CreateTime >= request.StartTime && x.CreateTime <= request.EndTime).ToList();
            }
            //总页数
            int toTalCount = list.Count();
            //分页数
            int toTalPage = (int)Math.Ceiling(toTalCount * 1.0 / request.TotalSize);
            //分页
            list = list.OrderByDescending(x => x.CreateTime).Skip((request.TotalIndex - 1) * request.TotalSize).Take(request.TotalSize).ToList();
            if (list.Count() < 0)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
                result.TotalCount = toTalCount;
                result.TotalPage = toTalPage;
                result.Items = list;
            }
            else
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.TotalCount = toTalCount;
                result.TotalPage = toTalPage;
                result.Items = list;
            }

            return await Task.FromResult(result);
        }
    }
}